﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Microsoft.Win32;

using Core.Model;
using Core.Model.Hierarchy;

namespace UI.Windows.Wizards
{
    public partial class NewProjectWizardWindow
    {
        public NewProjectWizardWindow(IEnumerable<IExaminationModel> models)
        {
            _models = models;
            DataContext = NewProject;

            InitializeComponent();
        }

        #region Public properties

        public readonly Project NewProject = Project.CreateProject();

        #endregion

        #region Private properties

        private readonly IEnumerable<IExaminationModel> _models;

        #endregion

        #region Event handlers

        private void AddCustomInformationButtonClick(object sender, RoutedEventArgs e)
        {
            if (TypeWatermarkTextBox.Text != String.Empty)
            {
                NewProject.CustomInformation.Add(new Tuple<string, string>(TypeWatermarkTextBox.Text, ContentWatermarkTextBox.Text));
            }
        }

        private void AddResearchButtonClick(object sender, RoutedEventArgs e)
        {
			using (var newResearchWizardWindow = new NewResearchWizardWindow(_models)
				{
					Owner = this,
					WindowStartupLocation = WindowStartupLocation.CenterOwner
				})
			{
				if ((bool)newResearchWizardWindow.ShowDialog()) 
				{
					NewProject.Researches.Add(newResearchWizardWindow.NewResearch);
				}
			}   
        }

        private void OpenSaveFileDialogButtonClick(object sender, RoutedEventArgs e)
        {
            var fileDialog = new SaveFileDialog
            {
                Title = "Choose location...",
                Filter = "All files (*.*)|*.*|Project files (*.raeproj)|*.raeproj",
                FilterIndex = 2
            };

            if (fileDialog.ShowDialog(this).Value)
            {
                NewProject.Path = fileDialog.FileName;
                PathTextBox.Text = fileDialog.FileName;
            }
        }

		private void WizardFinish(object sender, RoutedEventArgs e)
		{
			NewProject.CurrentResearch = NewProject.Researches.LastOrDefault();
		}

        #endregion
    }
}